//
//  MyDoctorViewController.m
//  HandheldHospital
//
//  Created by zhangchao on 16/3/5.
//  Copyright © 2016年 zhangchao. All rights reserved.
//

#import "MyDoctorViewController.h"
#import <DOPDropDownMenu.h>
#import "Hospital.h"
#import "DataManager.h"
#import <Masonry.h>


#define kWidth self.view.frame.size.width
#define kHeight self.view.frame.size.height
#define kShared [DataManager defaultManager]
#define kTFHeight (kHeight * 0.05)
#define kLWidth (kWidth * 0.15)
#define kTFWidth (kWidth - 20 - kLWidth)

@interface MyDoctorViewController ()<DOPDropDownMenuDataSource,DOPDropDownMenuDelegate,UITextFieldDelegate>
@property (nonatomic,strong) UITextField *doctornameTF;
@property (nonatomic,strong) UITextField *zhichengTF;
@property (nonatomic,strong) UITextField *keleiTF;
@property (nonatomic,strong) DOPDropDownMenu *yiyuanMenu;
@property (nonatomic,strong) NSString *doctorName;
@property (nonatomic,strong) NSString *zhicheng;
@property (nonatomic,strong) NSString *kelei;
@property (nonatomic,strong) NSString *yiyuan;
@property (nonatomic,strong) UILabel *nameLabel;
@property (nonatomic,strong) UILabel *zhichengLabel;
@property (nonatomic,strong) UILabel *keleiLabel;
@property (nonatomic,strong) UILabel *yiyuanLabel;
@end

@implementation MyDoctorViewController

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    //如果没有保存过信息
    if(kShared.seek == nil)
    {
        self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"保存" style:UIBarButtonItemStyleDone target:self action:@selector(saveBtn)];
    }
    else
    {
        self.nameLabel.textColor = [UIColor grayColor];
        self.zhichengLabel.textColor = [UIColor grayColor];
        self.keleiLabel.textColor = [UIColor grayColor];
        self.yiyuanLabel.textColor = [UIColor grayColor];
        self.doctornameTF.text = kShared.seek.name;
        self.zhichengTF.text = kShared.seek.zhicheng;
        self.keleiTF.text = kShared.seek.kelei;
    }
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"我的资料";
    self.tabBarController.tabBar.hidden = YES;
    self.view.backgroundColor = [UIColor whiteColor];
    [self addViews];
    
}


//页面布局
- (void)addViews
{
    self.nameLabel = [[UILabel alloc] init];
    self.nameLabel.text = @"姓名:";
    self.nameLabel.textAlignment = NSTextAlignmentCenter;
    [self.view addSubview:self.nameLabel];
    [self.nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view).offset(kHeight * 0.15);
        make.left.equalTo(self.view).offset(10);
        make.size.mas_equalTo(CGSizeMake(kLWidth, kTFHeight));
    }];
    
    
    self.doctornameTF = [[UITextField alloc] init];
    self.doctornameTF.placeholder = @"请输入真实姓名";
    self.doctornameTF.layer.borderWidth = 0.5;
    self.doctornameTF.layer.cornerRadius = 5;
    self.doctornameTF.tag = 401;
    self.doctornameTF.delegate = self;
    [self.view addSubview:self.doctornameTF];
    [self.doctornameTF mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view).offset(kHeight * 0.15);
        make.right.equalTo(self.view).offset(-10);
        make.size.mas_equalTo(CGSizeMake(kTFWidth, kTFHeight));
    }];
    
    self.zhichengLabel = [[UILabel alloc] init];
    self.zhichengLabel.text = @"职称:";
    self.zhichengLabel.textAlignment = NSTextAlignmentCenter;
    [self.view addSubview:self.zhichengLabel];
    [self.zhichengLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view).offset(kHeight * 0.15 + kTFHeight + 10);
        make.left.equalTo(self.view).offset(10);
        make.size.mas_equalTo(CGSizeMake(kLWidth, kTFHeight));
    }];
    
    self.zhichengTF = [[UITextField alloc] init];
    self.zhichengTF.placeholder = @"请输入职称";
    self.zhichengTF.layer.borderWidth = 0.5;
    self.zhichengTF.layer.cornerRadius = 5;
    self.zhichengTF.tag = 402;
    self.zhichengTF.delegate = self;
    [self.view addSubview:self.zhichengTF];
    [self.zhichengTF mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view).offset(kHeight * 0.15 + kTFHeight + 10);
        make.right.equalTo(self.view).offset(-10);
        make.size.mas_equalTo(CGSizeMake(kTFWidth, kTFHeight));
    }];
    
    
    self.keleiLabel = [[UILabel alloc] init];
    self.keleiLabel.text = @"科类:";
    self.keleiLabel.textAlignment = NSTextAlignmentCenter;
    [self.view addSubview:self.keleiLabel];
    [self.keleiLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view).offset(kHeight * 0.15 + 2 * kTFHeight + 20);
        make.left.equalTo(self.view).offset(10);
        make.size.mas_equalTo(CGSizeMake(kLWidth, kTFHeight));
    }];
    
    self.keleiTF = [[UITextField alloc] init];
    self.keleiTF.placeholder = @"请输入所属科类";
    self.keleiTF.layer.borderWidth = 0.5;
    self.keleiTF.layer.cornerRadius = 5;
    self.keleiTF.tag = 403;
    self.keleiTF.delegate = self;
    [self.view addSubview:self.keleiTF];
    [self.keleiTF mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view).offset(kHeight * 0.15 + 2 * kTFHeight + 20);
        make.right.equalTo(self.view).offset(-10);
        make.size.mas_equalTo(CGSizeMake(kTFWidth, kTFHeight));
    }];
    
    self.yiyuanLabel = [[UILabel alloc] init];
    self.yiyuanLabel.text = @"请点击下方选择所属医院";
    self.yiyuanLabel.textAlignment = NSTextAlignmentCenter;
    [self.view addSubview:self.yiyuanLabel];
    [self.yiyuanLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view).offset(kHeight * 0.15 + 3 * kTFHeight + 30);
        make.left.equalTo(self.view).offset(kWidth * 0.05);
        make.right.equalTo(self.view).offset(-(kWidth * 0.05));
        make.height.equalTo(@(kTFHeight));
    }];
    
    // 添加下拉菜单
    self.yiyuanMenu = [[DOPDropDownMenu alloc] initWithOrigin:CGPointMake(0, (kHeight * 0.15 + 4 * kTFHeight + 40)) andHeight:40];
    self.yiyuanMenu.delegate = self;
    self.yiyuanMenu.dataSource = self;
    [self.view addSubview:self.yiyuanMenu];
    [self.yiyuanMenu selectDefalutIndexPath];

}

//点击保存按钮的事件
- (void)saveBtn
{
    if(self.doctorName.length == 0 || self.zhicheng.length == 0 || self.kelei.length == 0)
    {
        [self showAlertOneButtonWithTitle:@"提示" AndMessage:@"请将资料补全" AndAlertStyle:UIAlertControllerStyleAlert AndOneButtonTitle:@"确定" AndBlock:nil];
        
    }
    else
    {
        SeekModel *seek = [SeekModel object];
        seek.currentUser = [AVUser currentUser];
        seek.name = self.doctornameTF.text;
        seek.kelei = self.kelei;
        seek.yiyuan = self.yiyuan;
        seek.zhicheng = self.zhicheng;
        [seek saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
            if(succeeded)
            {
                [self.navigationController popViewControllerAnimated:YES];
            }
            else
            {
                [self showAlertOneButtonWithTitle:@"提示" AndMessage:@"未知错误，保存失败" AndAlertStyle:UIAlertControllerStyleAlert AndOneButtonTitle:@"确定" AndBlock:nil];
            }
        }];
    }
    
}

#pragma mark文本框代理
- (void)textFieldDidEndEditing:(UITextField *)textField
{
    if(textField.tag == 401)
    {
        self.doctorName = textField.text;
    }
    if(textField.tag == 402)
    {
        self.zhicheng = textField.text;
    }
    if(textField.tag == 403)
    {
        self.kelei = textField.text;
    }
}



#pragma mark 下拉菜单的代理
/**
 *  返回 menu 第column列有多少行
 */
- (NSInteger)menu:(DOPDropDownMenu *)menu numberOfRowsInColumn:(NSInteger)column
{
    if(kShared.seek != nil)
    {
        return 0;
    }
    return kShared.hospitalArray.count;
}

/**
 *  返回 menu 第column列 每行title
 */
- (NSString *)menu:(DOPDropDownMenu *)menu titleForRowAtIndexPath:(DOPIndexPath *)indexPath
{
    //如果医生信息已经有了。直接显示当前信息
    if(kShared.seek != nil)
    {
        return kShared.seek.yiyuan;
    }
    if(kShared.hospitalArray.count == 0)
    {
        return  nil;
    }
    Hospital *hospital = kShared.hospitalArray[indexPath.row];
    self.yiyuan = hospital.hospitalName;
    return hospital.hospitalName;
}

//点击屏幕让键盘回收
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [self.doctornameTF resignFirstResponder];
    [self.zhichengTF resignFirstResponder];
    [self.keleiTF resignFirstResponder];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
